<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-10
 * Time: 11:47
 * 创建会员信息操作
 */

namespace app\api\service\member;


use app\api\model\Member;
use app\api\model\User;
use app\api\service\Token;
use app\lib\exception\ParameterException;
use think\Db;
use think\Exception;
use think\facade\Cache;

class MemberSer extends Token
{
    protected $postArr;

    protected $user;


    function __construct($post)
    {
        $this->postArr = $post;
    }

    // 入口
    public function get()
    {
        $uid = self::getCurrentUid();
        $this->user = User::where('id',$uid)->with('member')->find();
        // 判断验证码
        $this->checkPhoneCode();
        if($this->user['member']) {
            return $this->updateData();
        } else {
            return $this->createData();
        }
    }

    // 创建数据
    private function createData()
    {
        Db::startTrans();
        try{
            $this->postArr['user_id'] = $this->user['id'];
            $mode = Member::create($this->postArr);
            $member_no = $this->getMemberNo($mode->id);
            Member::where('id',$mode->id)->update([
                'member_no' => $member_no
            ]);
            Db::commit();
            return writeJson(201, [], '添加成功！');
        }
        catch (Exception $ex) {
            //回滚事务
            Db::rollback();
            throw $ex;
        }
    }

    // 获取会员编号
    private function getMemberNo($member_id)
    {
        $date = date('Ymd');
        $string = $this->getString($member_id);
        return $date.$string;
    }


    // 根据会员ID得到随机数
    private function getString($member_id)
    {
        $number = 6 - strlen($member_id);
        if($number > 0) {
            return getRandNumber($number).$member_id;
        } else {
            return $member_id;
        }
    }

    // 更新数据
    private function updateData()
    {
        if($this->user['member']['status'] == 3) {
            throw new ParameterException([
                'msg' => [
                    'member' => '您已经被列入黑名单'
                ]
            ]);
        }
        if($this->user['member']['status'] == 4) {
            throw new ParameterException([
                'msg' => [
                    'member' => '信息已经审核通过'
                ]
            ]);
        }
        Db::startTrans();
        try{
            $member = new Member();
            $this->postArr['status'] = 1;
            $member->allowField(true)->save($this->postArr,[ 'id' => $this->user['member']['id'] ]);
            Db::commit();
            return writeJson(202, [], '修改成功！');
        }
        catch (Exception $ex) {
            //回滚事务
            Db::rollback();
            throw $ex;
        }
    }

    // 判断验证码的正确
    private function checkPhoneCode()
    {
        $data = Cache::get($this->user['id']);
        if(!$data) {
            throw new ParameterException([
                'msg' => [
                    'member' => '请点击获取验证码'
                ]
            ]);
        }
        if($data['phone'] != $this->postArr['phone']) {
            throw new ParameterException([
                'msg' => [
                    'member' => '获取验证码的手机号和该手机号不匹配'
                ]
            ]);
        }
        if($data['code'] != $this->postArr['phone_code']) {
            throw new ParameterException([
                'msg' => [
                    'member' => '验证码不正确'
                ]
            ]);
        }
    }
}